<?php

namespace app\admin\controller;

use cmf\controller\AdminBaseController;
use think\Controller;
use think\Request;
use think\Db;
use think\db\Query;
use Qiniu\Auth;
use Qiniu\Storage\UploadManager;

class RecordController extends AdminBaseController
{
    //显示
    public function index()
    {
        //搜索条件
        $name=$this->request->param('name');
        //连接数据库可以去掉表前缀，此方法可自己读取数据库配置文件中的表前缀
        $record=Db::name('record')
            //表连接
            ->join('cmf_student','cmf_record.student_id=cmf_student.id','LEFT')
            ->join('cmf_reason','cmf_record.reason_id=cmf_reason.id','LEFT')
            ->join('cmf_score','cmf_record.score_id=cmf_score.id','LEFT')
            ->join('cmf_user','cmf_record.user_id=cmf_user.id','LEFT')
            //查询数据
            ->field('cmf_record.*')
            ->field('cmf_student.name as stu_name,cmf_reason.name as reason_name,cmf_score.name as score_name,cmf_user.user_login as user_name')
            //接收所查询的值,使用where的闭包函数查询
            ->where(function(Query $query) use ($name){
                //学生姓名搜索
                if($name){
                    $query->where('cmf_student.name','like',"%$name%");
                }
            })   
            //倒序排列（ASC为正序）      
            ->order("id DESC")
            //分页：每页条数，跳转路径，额外参数
            ->paginate(3, false, ['query' => request()->param()]);
        //渲染分页
        $page=$record->render();
        //渲染页面，传参
        return view('index',['record'=>$record,'page'=>$page]);
    }

    //添加
    public function add(Request $request)
    {
        //判断提交类型
        if ($this->request->isPost()){
            //获取当前id提交的全部数据           
            $data=input();
            //添加数据
            $add=Db::name('record')->insert($data);
            //判断是否添加成功
            if ($add){
                $this->success('添加成功','record/index');
            }else{
                $this->error('添加失败');
            }
        }
        //查询积分列表中的信息
        $score = Db::name('score')->select();
        //查询原因列表中的信息
        $reason = Db::name('reason')->select();
        //查询学校列表中的信息
        $school = Db::name('school')->select();
        //渲染页面，传参
        return view('record_add',['school' => $school, 'score' => $score, 'reason' => $reason]);
    }

    //获取当前学校下的所有班级
    public function getRoom()
    {
        //获取当前学校id
        $schoolId = input('school_id');
        //查询此学校下的所有班级
        $room = Db::name('room')->where('school_id',$schoolId)->select();
        //判断此学校下是否有班级
        if ($room){
            //定义一个空数组
            $options = ["<option value=''>请选择班级</option>"];
            //遍历输出
            foreach ($room as $v){
                //选中学校后对应的班级值
                $opt = '<option class = "form-control" value="'.$v['id'].'">'.$v['name'].'</option>';
                //使用array_push函数向第一个参数的数组尾部添加一个或多个元素，然后返回新数组的长度（填充数组）
                array_push($options, $opt);
            }
            //返回输出
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }

    //获取当前班级下的所有学生
    public function getStudent()
    {
        //获取当前班级id
        $roomlId = input('room_id');
        //查询此班级下的所有学生
        $student = Db::name('student')->where('room_id',$roomlId)->select();
        //判断此班级下是否有学生
        if ($student){
            //定义一个空数组
            $options = ["<option value=''>请选择学生</option>"];
            //遍历输出
            foreach ($student as $v){
                //选中班级后对应的学生值
                $opt = '<option class = "form-control" value="'.$v['id'].'">'.$v['name'].'</option>';
                //使用array_push函数向第一个参数的数组尾部添加一个或多个元素，然后返回新数组的长度（填充数组）
                array_push($options, $opt);
            }
            //返回输出
            return $options;
        }else{
            return "<option value=''>暂无数据</option>";
        }
    }

    //编辑
    public function edit($id)
    {
        //判断提交类型，是否为post提交
        if ($this->request->isPost()) {
            //获取提交数据
            $data = input();
            //删除图片变量
            unset($data['photo_name']);
            //获取数据库中的id，修改数据
            $edit = Db::name('record')->update($data,['id'=>$data['id']]);
            //判断是否修改成功
            if ($edit) {
                $this->success('修改成功','record/index');
            } else {
                $this->error('修改失败');
            }
        }
        //获取要编辑的积分记录列表id
        $manage = Db::name('record')->get(['id'=>$id]);
        //获取所对应的学生id
        $student_id = $manage['student_id'];
        //表连接
        $stu = Db::name('student')->join( 'cmf_room',' cmf_room.id=cmf_student.room_id','LEFT')
            ->join( 'cmf_school',' cmf_school.id=cmf_room.school_id','LEFT')
            //查询数据
            ->field('cmf_student.id')
            ->field('cmf_room.id as room_id')
            ->field('cmf_school.id as school_id')
            ->where(['cmf_student.id'=>$student_id])
            ->select();
        $stu=$stu[0];
        //将获取的数据重新赋值给变量
        $school_id = $stu['school_id'];
        $manage['school_id'] = $school_id;
        $room_id = $stu['room_id'];
        $manage['room_id'] = $room_id;
        //获取学校列表中的所有信息
        $school = Db::name('school')->all();
        //获取当前学校下的所有班级
        $room = Db::name('room')->all(['school_id'=>$stu['school_id']]);
        //获取当前班级下的所有学生
        $student = Db::name('student')->all(['room_id'=>$stu['room_id']]);
        //获取积分列表中的所有信息
        $score = Db::name('score')->all();
        //获取原因列表中的所有信息
        $reason = Db::name('reason')->all();
        //渲染页面，传参
        return view( 'record_edit' ,['school'=>$school,'student' =>$student,'room'=>$room,'reason'=>$reason,'score'=>$score,'manage'=>$manage]);
    }
    
    //删除
    public function delete()
    {
        //获取当前id对应的全部数据
        $data=input();
        //获取数据库中的id，删除数据
        $del=RecordModel::destroy($data['id']);
        //判断是否删除成功
        if($del){
            $this->success('删除成功','record/index');
        }else{
            $this->error('删除失败');
        }
    }
}